﻿Imports System.Text
Imports System.Text.RegularExpressions
Imports System.Web.UI.WebControls
Imports Microsoft.VisualBasic

Public Class CheckUtil

    Public Shared Function IsEmpty(ByVal ctl As TextBox) As Boolean

        If IsNothing(ctl) Then
            Return False
        End If

        If String.IsNullOrEmpty(ctl.Text.Trim()) Then
            Return False
        End If

        Return True

    End Function

    Public Shared Function IsEmpty(ByVal ctl As DropDownList) As Boolean
        If IsNothing(ctl) Then
            Return False
        End If

        If String.IsNullOrEmpty(ctl.SelectedItem.Text.Trim()) Then
            Return False
        End If

        Return True
    End Function

    Public Shared Function IsEmpty(ByVal ctl As CheckBox) As Boolean
        If IsNothing(ctl) Then
            Return False
        End If

        If ctl.Checked = False Then
            Return False
        End If

        Return True
    End Function

    Public Shared Function IsNumber(ByVal ctl As TextBox) As Boolean
        If IsNothing(ctl) Then
            Return False
        End If

        Dim strText As String = ctl.Text.Trim().Replace(",", "")

        If Not IsNumeric(strText) Then
            Return False
        End If

        Return True
    End Function

    Public Shared Function IsDateTime(ByVal ctl As TextBox) As Boolean
        If IsEmpty(ctl) Then
            Return False
        End If

        Dim strText As String = ctl.Text.Trim()
        Dim strReg As String = "[1-2]{1}[0-9]{3}((-|\/){1}(([0]?[1-9]{1})|(1[0-2]{1}))((-|\/){1}((([0]?[1-9]{1})|([1-2]{1}[0-9]{1})|(3[0-1]{1})))( (([0-1]{1}[0-9]{1})|2[0-3]{1}):([0-5]{1}[0-9]{1}):([0-5]{1}[0-9]{1})(\.[0-9]{3})?)?)?)?$"

        Dim reg As New Regex(strReg)

        If Regex.IsMatch(strText, strReg) Then
            Dim _IndexY As Integer = -1
            Dim _IndexM As Integer = -1
            Dim _IndexD As Integer = -1

            If -1 <> (_IndexY = strText.IndexOf("/")) Then
                _IndexY = strText.IndexOf("/")
                _IndexM = strText.IndexOf("/", _IndexY + 1)
                _IndexD = strText.IndexOf(":")
            End If

            If -1 = _IndexM Then
                Return True
            End If

            If -1 = _IndexD Then
                _IndexD = strText.Length + 3
            End If

            Dim iYear As Integer = Convert.ToInt32(strText.Substring(0, _IndexY))
            Dim iMonth As Integer = Convert.ToInt32(strText.Substring(_IndexY + 1, _IndexM - _IndexY - 1))
            Dim iDate As Integer = Convert.ToInt32(strText.Substring(_IndexM + 1, _IndexD - _IndexM - 4))

            If (iMonth < 8 And (1 = iMonth Mod 2)) Or (iMonth > 8 And (0 = iMonth Mod 2)) Then
                If iDate < 32 Then
                    Return True
                End If
            Else
                If iMonth <> 2 Then
                    If iDate < 31 Then
                        Return True
                    End If
                Else
                    If (0 = iYear Mod 400) Or ((0 = iYear Mod 4) And (0 < iYear Mod 100)) Then
                        If iDate < 30 Then
                            Return True
                        End If
                    Else
                        If iDate < 29 Then
                            Return True
                        End If
                    End If
                End If
            End If
        End If

        Return False
    End Function

    Public Shared Function IsOverMaxLenght(ByVal ctl As TextBox, ByVal ime As Ime) As Boolean

        If IsEmpty(ctl) Then
            Return True
        End If

        Dim intMaxLength As Integer = 0

        If ctl.MaxLength = 0 Then
            Return True
        End If

        Select Case ime
            Case CheckUtil.Ime.ZENKAKU
                intMaxLength = ctl.MaxLength * 2
            Case CheckUtil.Ime.KONZAI
                intMaxLength = ctl.MaxLength
            Case CheckUtil.Ime.HANKAKU
                intMaxLength = ctl.MaxLength
        End Select

        Dim intByteCount As Integer = Encoding.Default.GetByteCount(ctl.Text)

        If intByteCount > intMaxLength Then
            Return True
        End If

        Return False
    End Function

    Public Shared Function IsOverDigit(ByVal ctl As TextBox, ByVal integerDigit As Integer, ByVal decimalDigit As Integer)

        If IsEmpty(ctl) Then
            Return True
        End If

        If IsNumber(ctl) = False Then
            Return True
        End If

        Dim strText As String = ctl.Text.Trim().Replace(",", "")

        Dim dotIndex As Integer = strText.IndexOf(".")
        Dim integerText As Integer = strText.Substring(0, dotIndex)
        Dim decimalText As Integer = strText.Substring(dotIndex + 1)

        If integerDigit > integerText Then
            Return True
        End If

        If decimalDigit > decimalText Then
            Return True
        End If

        Return False

    End Function

    Public Enum Ime
        ZENKAKU = 1
        HANKAKU = 2
        KONZAI = 3
    End Enum

End Class
